#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;

int n, m;
ll a[N];
ll b[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) a[i] += a[i - 1];
  ll x = a[n] / n;
  rep(i, 1, n) b[i] = a[i] - i * x;
  sort(b + 1, b + n + 1);
  x = b[(1 + n) / 2];
  ll ans = 0;
  rep(i, 1, n) ans += abs(x - b[i]);
  cout << ans;
  return 0;
}